package com.atgugui.edustatistics.service.impl;


import com.atgugui.commonutils.Result;
import com.atgugui.commonutils.ResultCode;
import com.atgugui.edustatistics.client.UcenterClient;
import com.atgugui.edustatistics.entity.StatisticsDaily;
import com.atgugui.edustatistics.mapper.StatisticsDailyMapper;
import com.atgugui.edustatistics.service.StatisticsDailyService;
import com.atgugui.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-17
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void countRegister(String day) {

        // 先删除旧数据，然后在插入新的数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

        Result result = ucenterClient.countRegister(day);
        int count = (int) result.getData().get("registerCount");
        // 将统计出来的结果存放到数据库表中
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setDateCalculated(day);

        // TODO 统计每日播放视频数
        statisticsDaily.setLoginNum(10);

        // TODO 统计登录人数
        statisticsDaily.setVideoViewNum(5);

        // TODO 统计每日新增课程数
        statisticsDaily.setCourseNum(10);

        baseMapper.insert(statisticsDaily);
    }

    @Override
    public HashMap<String, Object> statisticsData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end); // 设置统计的起始时间和结束时间
        queryWrapper.select(type, "date_calculated");
        List<StatisticsDaily> statisticsDailyList = baseMapper.selectList(queryWrapper);
        HashMap<String,Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<>(); // 存放数据
        List<String> dateList = new ArrayList<>(); // 存放日期
        map.put("dataList", dataList);
        map.put("dateList", dateList);
        for (StatisticsDaily daily : statisticsDailyList) {
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        return map;
    }
}
